import os
import re
import sys
import time
import optparse
import subprocess
import ctypes
import string

cmd = os.system
sleep = time.sleep

app_name = 'com.baidu.browser.apps'
main_activity = 'com.baidu.browser.apps/com.baidu.browser.framework.BdBrowserActivity'
pid_index = 1

#sina
defaultPixel = 600
maxDeltaPixel = 2000
maxPixel = 12000
maxSwipeTimes = 6
dragTime = 3000

def get_pid():
    print("-------------get pid------------")
    ret = os.popen('adb shell ps | findstr ' + app_name) 
    sleep(2)
    while True:        
        title = ret.readline().split()
        if len(title) != 0:
            if ':' not in title[len(title)-1] and 'data' not in title[len(title)-1]:
			    #print title
			    pid = title[pid_index]
			    return pid
        else:
            print("can't connect to device")
            time.sleep(5)
            ret = os.popen('adb shell ps | findstr ' + app_name)

if __name__ == "__main__":
	start_time = time.time()
	if len(sys.argv) > 1:
		if sys.argv[1] == 'blink':	
		    app_name = 'com.baidu.browser.blink'
		    main_activity = 'com.baidu.browser.blink/com.baidu.browser.framework.BdBrowserActivity'	
		    print 'open blink version'	
		elif sys.argv[1] == 'qq':	
		    app_name = 'com.tencent.mtt'
		    main_activity = 'com.tencent.mtt/.SplashActivity'
		    print 'open qq browser'	
		elif sys.argv[1] == 'uc':	
		    app_name = 'com.UCMobile'
		    main_activity = 'com.UCMobile/com.uc.browser.InnerUCMobile'
		    print 'open UC browser'	
		elif sys.argv[1] == 'org':	
		    app_name = 'org.chromium.android_webview.shell'
		    main_activity = 'org.chromium.android_webview.shell/.AwShellActivity'
		    print 'open org browser'			
		elif sys.argv[1] == 'test':	
		    app_name = 'com.baidu.zeustest'
		    main_activity = 'com.baidu.zeustest/.MainActivity'
		    print 'open zeustest browser'		
	'''
	cmd('adb shell am force-stop ' + app_name)
	#sina
	cmd('adb shell am start -n '+ main_activity + ' -a android.intent.action.VIEW -d http://sina.cn' )
	'''
	print 'start'
	deltaPixel = defaultPixel
	repeat = 1
	isDrag = 0
	#Swipe Delta Pixel
	if len(sys.argv) > 2:
		deltaPixel *= string.atoi(sys.argv[2])
		if deltaPixel > maxDeltaPixel:
		    deltaPixel = maxDeltaPixel
		print 'swipe delta = ' + str(deltaPixel)
	#Drag flag: default fling, drag if value > 0
	if len(sys.argv) > 3:
		isDrag = string.atoi(sys.argv[3])
	sleep(1)
	swipeTimes = maxPixel/deltaPixel
	if swipeTimes > maxSwipeTimes:
	    swipeTimes = maxSwipeTimes
	if(isDrag > 0):
		print 'Play slow drag command'
		deltaPixel = defaultPixel#stick drag deltaPixel
		print 'Real swipe delta = ' + str(deltaPixel)
		swipeTimes = swipeTimes / 3	
	for i in range(repeat):	
	    for j in range(swipeTimes):
	        if isDrag == 0:	
	    	    cmd('adb shell input swipe 400 ' + str(100+deltaPixel) + ' 400 100')
	        else:
	    	    cmd('adb shell input touchscreen swipe 400 ' + str(100+maxDeltaPixel) + ' 400 100 ' + str(dragTime))
	        print 'up'
	    for k in range(swipeTimes):
	        if isDrag == 0:	
	    	    cmd('adb shell input swipe 400 400 400 ' + str(400+deltaPixel))#temp
	        else:
	    	    cmd('adb shell input touchscreen swipe 400 400 400 ' + str(400+maxDeltaPixel) + ' ' + str(dragTime))
	        #cmd('adb shell input swipe 400 200 400 ' + str(200+deltaPixel))		
	        print 'down'
	end_time = time.time()
	print 'Time total = ' + str(end_time - start_time) + 's'
	#record gfxinfo
	#browser-master
	#adb shell dumpsys gfxinfo com.baidu.browser.apps > sina_master_fling_slow.txt
	#adb shell dumpsys gfxinfo com.baidu.browser.apps > sina_master_fling_quick.txt
	#browser-T7 by hand
	#adb shell dumpsys gfxinfo com.baidu.browser.apps > sina_master_drag_slow.txt
	
	#browser-commit
	#adb shell dumpsys gfxinfo com.baidu.browser.apps > sina_commit_fling_slow.txt
	#adb shell dumpsys gfxinfo com.baidu.browser.apps > sina_commit_fling_quick.txt
	#browser-T5 by hand
	#adb shell dumpsys gfxinfo com.baidu.browser.apps > sina_commit_drag_slow.txt
	
	#browser-QQ-beta
	#adb shell dumpsys gfxinfo com.tencent.mtt > sina_QQbeta_fling_slow.txt
	
	#print pid for systrace
	print 'PID = ' + str(get_pid())